package com.bjpowernode.handler;

import com.bjpowernode.constant.Constant;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 自定义RejectedExecutionHandler
 *
 * @author Cat老师，关注我，抖音搜索：java512
 */
public class MySpecialRejectedExecutionHandler implements RejectedExecutionHandler {

    private RedisTemplate<Object, Object> redisTemplate;

    public MySpecialRejectedExecutionHandler(RedisTemplate<Object, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        //1.记录日志 (暂时用控制台打印，你可以用日志工具完善一下：log4j2)
        System.err.println("Task " + r.toString() + " rejected from " + executor.toString());
        //2.将任务持久化到redis中
        this.redisTemplate.opsForList().leftPush(Constant.TASK_KEY, r);
    }
}
